<template>
  <div>
    <el-card>
      <!-- 顶部面包屑标识与导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }">
          <i class="el-icon-s-promotion"></i> 后台管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>景点产品管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-divider></el-divider>
      <el-row  style="margin-top: 10px">
        <el-button type="primary" @click="openAddNewDialog()"  style="margin-left: 10px">新增</el-button>
        <download-excel
            style="float: left"
            class="export-excel-wrapper"
            :data="spotData"
            type="xls"
            worksheet="worksheet"
            name="用户信息">
          <el-button type="primary">导出</el-button>
        </download-excel>
      </el-row>
      <el-table
          :data="spotData"
          style="width: 100%">
        <el-table-column
            prop="id"
            label="景点编号"
            width="180">
        </el-table-column>
        <el-table-column
            prop="name"
            label="景点名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="location"
            label="景点地址">
        </el-table-column>
        <el-table-column
            prop="phone"
            label=景点电话>
        </el-table-column>
        <el-table-column
            prop="categoryName"
            label="类型名称">
        </el-table-column>
        <el-table-column
            prop="price"
            label="价格">
        </el-table-column>
        <el-table-column label="操作"  width="200px"
                         align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini"
                       @click="openEditDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini"
                       @click="openDeleteConfirm(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 修改数据的表单 -->
    <el-dialog title="编辑用户管理数据" :visible.sync="editFormVisible">
      <el-form :model="editForm"  label-width="120px">
        <el-form-item label="景点名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="景点地址" prop="location">
          <el-input v-model="editForm.location"></el-input>
        </el-form-item>
        <el-form-item label="景点电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
<!--        <el-form-item label="类型名称" prop="categoryName">
          <el-cascader
              v-model="editForm.parentId"
              :options="categoryDataOptions"
              :props="{checkStrictly: true}"
              clearable="true"
              @change="handleChange"></el-cascader>
        </el-form-item>-->
        <el-form-item label="价格" prop="price">
          <el-input v-model="editForm.price"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 新增数据的表单 -->
    <el-dialog title="新增角色数据" :visible.sync="FormVisible">
      <el-form :model="editForm"  label-width="120px">
        <el-form-item label="景点名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="景点地址" prop="location">
          <el-input v-model="editForm.location"></el-input>
        </el-form-item>
        <el-form-item label="景点电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="类型名称" prop="categoryName">
          <el-cascader
              v-model="editForm.parentId"
              :options="categoryDataOptions"
              :props="{checkStrictly: true}"
              clearable="true"
              @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="editForm.price"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="FormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSpot()">确 定</el-button>
      </div>
    </el-dialog>
    <el-pagination
        @current-change="changePage"
        layout="total, prev, pager, next"
        :hide-on-single-page="true"
        :page-size="pageSize"
        :current-page="currentPage"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "HotelProduct",
  data:function () {
    return {
      // 分页相关数据
      currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
      pageSize: '',
      total:'',
      categoryDataOptions:'',
      editFormVisible: false,
      FormVisible: false,
      spotData: [],
      editForm: {
        id: '',
        name: '',
        location: '',
        phone: '',
        categoryId: '',
        price:'',
      },
    }
  },
  methods:{
    // 打开新增对话框
    openAddNewDialog(tableItem) {
      this.loadCategoryTreeList();
      this.FormVisible = true
    },
    // 执行新增
    addSpot() {
      let url = 'http://localhost:8080/v1/spots/new-add';
      console.log('url = ' + url);
      if (Array.isArray(this.editForm.parentId)) {
        this.editForm.parentId = this.editForm.parentId.pop();
      }
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code ==1) {
          this.$message({
            message: '新增景点成功！',
            type: 'success'

          });
          this.FormVisible = false;
          this.loadSpotList();
        } else if (jsonResult.code== 4040) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.FormVisible = false;
            }
          });
        } else {
          let title = '操作失败,新增的景点已存在!';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    handleChange(value) {
      this.editForm.categoryId=value[0];
    },
   loadCategoryTreeList() {
      let url = 'http://localhost:8080/v1/category/tree/?id=5';

      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code === 1) {
          this.categoryDataOptions = jsonResult.data;
          console.log(jsonResult.data.list)
        }
      }).catch(err => {
        let message = '服务器响应错误！';
        this.$alert(message, '操作失败', {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
      });
    },
    // 弹出修改对话框
    openEditDialog(tableItem) {
      /*this.loadCategoryTreeList();*/
      console.log(tableItem)
      let url = 'http://localhost:8080/v1/admins/' + tableItem.id+"/spot";
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log('jsonResult ='+ jsonResult.data)
        if (jsonResult.code == 1) {
          this.editForm = jsonResult.data;
          this.editForm.categoryId=tableItem.categoryId
          console.log(this.editForm.categoryId)
          this.editFormVisible = true;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    // 执行修改
    handleEdit() {
      let url = 'http://localhost:8080/v1/spots/' + this.editForm.id + '/updateSpot';
      console.log('url = ' + url);/*
      if (Array.isArray(this.editForm.parentId)) {
        this.editForm.parentId = this.editForm.parentId.pop();
      }*/

      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code ==1) {
          this.$message({
            message: '修改景点成功！',
            type: 'success'
          });
          this.editFormVisible = false;
          this.loadSpotList();
        } else if (jsonResult.state == 4040) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.editFormVisible = false;
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    // 打开删除确认框
    openDeleteConfirm(role) {
      let message = '此操作将永久删除【' + role.name + '】景点，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(role);
      }).catch(() => {
      });
    },
    // 处理删除
    handleDelete(spot) {
      let url = 'http://localhost:8080/v1/spots/' + spot.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code == 1) {
          this.$message({
            message: '删除【' + spot.name + '】景点成功！',
            type: 'success'
          });
          this.loadSpotList();
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },
    roleHandleEdit(index,user){
      console.log(index);
    },
    roleHandleDelete(){
      console.log(index);
    },
    changePage(page) {
      this.$router.replace('?page=' + page);
      this.loadSpotList();
    },
    loadSpotList(){
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }
      let url = 'http://localhost:8080/v1/spots/list?page='+page;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response)=>{
        let jsonResult = response.data;
        if(response.data.code==1){
          console.log(jsonResult)
          this.spotData = jsonResult.data.list;
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
          console.log(this.spotData)
        }else{
          this.$message.error(jsonResult.msg)
        }
      })
    },
  },
  mounted() {
    this.loadSpotList();
  }
}
</script>

<style scoped>

</style>